<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 * 餐桌分类
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'table';
$store_id=$this->session('store_id');
if(!$store_id){
	exi('请先选择场馆！','error');
}
if($_RQ['op']=='table'){
	$this->session('order_type',1);
	$params=array();
	$params['uniacid']=$_SESSION['uniacid'];
	$params['store_id']=$store_id;
	if(!empty($_RQ['title'])){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$fields=array('id','title','group_id','status','busy','image');
	$list=pdo_getslice('jy_qiuyuyue_table',$params,array($pindex,$psize),$total,$fields,'','id desc');
	$return['busys']=array(
		'0'=>'空闲',
		'1'=>'已入座',
		'2'=>'已下单',
		'3'=>'已付款',
	);
	if($list){
		foreach($list as &$item){
			$item['table_type']=$return['busys'][$item['busy']];
			$group = pdo_get('jy_qiuyuyue_table_group',array('uniacid'=>$_SESSION['uniacid'],'store_id'=>$store_id,'id'=>$item['group_id']));
			$item['groupname']=$group['title'];
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$return['groups']=pdo_getall('jy_qiuyuyue_table_group',array('uniacid'=>$_SESSION['uniacid'],'store_id'=>$store_id),array('id','title'),'id');
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='clear'){
	//清台
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择餐桌！','error');
	}
	pdo_update('jy_qiuyuyue_table',array('busy'=>0),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	exi('操作成功！','','table');
}
if($_RQ['op']=='out'){
	$this->session('order_type',2);
	exi('','','jy_qiuyuyue/s_posgoods/');
}
if($_RQ['op']=='self'){
	$this->session('order_type',3);
	exi('','','jy_qiuyuyue/s_posgoods/');
}
if($_RQ['op']=='yuding'){
	$this->session('order_type',5);
	exi('','','jy_qiuyuyue/s_posgoods/');
}
if($_RQ['op']=='set'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择餐桌！','error');
	}
	$update=array();
	$post=get_postdata();
	if(isset($post['status'])){
		$update['status']=intval($post['status']);
	}
	if($update){
		pdo_update('jy_qiuyuyue_table',$update,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}
	exi('操作成功！');
}
if($_RQ['op']=='edit'){
	$return=array();
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('jy_qiuyuyue_table',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		
	}else{
		$item=array();
	}
	$return['item']=$item;
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'tabs'=>array(
			array('op'=>'list','title'=>'返回餐桌类型列表'),
		),
		'edits'=>array(
			array(
				'title'=>'基本信息',
				'list'=>array(
					array('type'=>'string','title'=>'餐桌编号','name'=>'title','help'=>''),
					array('type'=>'select','title'=>'餐桌类型','name'=>'group_id','help'=>'','options'=>'groups'),
					array('type'=>'image','title'=>'餐桌图片','name'=>'image','help'=>''),
					array('type'=>'switch','title'=>'是否启用','name'=>'status','help'=>''),
				)
			),
		)
	);
	$return['groups']=pdo_getall('jy_qiuyuyue_table_group',array('uniacid'=>$_SESSION['uniacid'],'store_id'=>$store_id),array('id','title'),'id');
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	$data=array(
		'title'=>trim($postdata['title']),
		'group_id'=>intval($postdata['group_id']),
		'status'=>intval($postdata['status']),
		'image'=>trim($postdata['image']),
	);
	$id=intval($postdata['id']);
	if($id){
		pdo_update('jy_qiuyuyue_table',$data,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}else{
		$data['uniacid']=$_SESSION['uniacid'];
		$data['store_id']=$store_id;
		pdo_insert('jy_qiuyuyue_table',$data);
	}
	exi('操作成功！','','list');
}
if($_RQ['op']=='del'){
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
	}else{
		$del=array();
	}
	if($_RQ['id']){
		$del[]=$_RQ['id'];
	}
	foreach($del as $id){
		$id=intval($id);
		if($id){
			pdo_delete('jy_qiuyuyue_table',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}
	}
	exi('删除成功！');
}